import 'package:cat_eye/base/base_chang_notifier.dart';
import 'package:cat_eye/bean/home_body_bean.dart';
import 'package:cat_eye/config/url_config.dart';
import 'package:cat_eye/http/http_manager.dart';
import 'package:cat_eye/util/loading_state_widget_util.dart';
import 'package:flutter/material.dart';

/*
* @Package: view_model.home_view_model
* @ClassName: home_body_view_model
* @Author: szj
* @CreateDate: 7/28/21 3:48 PM
*/
class HomeBodyViewModel extends BaseChangNotifier {
  List<ItemList> bannerList = [];

  //请求网络数据
  void requestHttpData() {
    if (bannerList != null && bannerList.length > 0) {
      return;
    }
    HttpManager.requestHttpData(
      UrlConfig.feedUrl,
      onSuccess: (s) {
        type = LoadingWidget.DONG;

        var bean = HomeBodyBean.fromJson(s);
        //拿到Banner数据
        bean.issueList[0].itemList.forEach((element) {
          if (element.type != "banner2" && bannerList != null) {
            bannerList.add(element);
          }
        });
      },
      onFail: (s) {
        type = LoadingWidget.ERROR;
      },
      complete: () => notifyListeners(),
    );
  }

  //加载网络数据
  void retry() {
    //默认进度条是加载中
    type = LoadingWidget.LOADING;
    notifyListeners();

    //加载网络数据时，重新加载数据
    requestHttpData();
  }
}
